package com.vpen.blog.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/7/11 16:47
 * -@Content: Rabbit的配置类
 */
@Configuration
public class RabbitMQConfig {

    // 使用延迟插件 插件使用了数据库来存消息，扫瞄里面的时间，将过期的放入队列
    // 消费者监听到队列中有消息就会消费

    // 只需声明
        // 1个交换机
        // 1个路由
        // 1个队列
    // 来实现延迟队列

    // 延迟交换机
    public static final String Delay_Exchange_Name = "delay.exchange";
    //延迟队列
    public static final String Delay_Queue_Name = "delay.queue";
    // 延迟路由
    public static final String Delay_Queue_Routing_key = "delay.queue.routingKey";

    //声明延迟交换机
    @Bean("delayExchange")
    public CustomExchange delayExchange(){
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(Delay_Exchange_Name,"x-delayed-message",true,false,args);
    }

    @Bean("delayQueue")
    public Queue delayQueue(){
        return new Queue(Delay_Queue_Name);
    }

    // 绑定交换机和队列
    @Bean
    public Binding delayBinding(@Qualifier("delayQueue") Queue queue,
                                @Qualifier("delayExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(Delay_Queue_Routing_key).noargs();

    }

}
